package com.cskaoyan.javase.reflect._6demo;

import java.lang.reflect.Field;

/**
 * 父类中私有成员被子类隐式继承
 * @since 11:41
 * @author wuguidong@cskaoyan.onaliyun.com
 */
public class Demo {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        // 获取Son类的Class对象
        Class<? extends Son> sonClazz = new Son().getClass();
        // getDeclaredField只能获取同类下的成员变量
        // Field num = sonClazz.getDeclaredField("num");
        // Son对象的Class对象存在,Father的Class对象一定也存在
        Class<?> fatherClazz = sonClazz.getSuperclass();
        Field numField = fatherClazz.getDeclaredField("num");

        Son s = new Son();
        numField.setAccessible(true);
        numField.set(s, 1000);
        System.out.println(numField.get(s));
    }
}

class Father {
    private int num;
    public int num2;
}

class Son extends Father {

}
